<?php
 
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 karPenilaian_Student
	 karPenilaian_Student_add
	 karPenilaian_Student_edit
	 */
	class Isi_presensiController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Akademik";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("akademik")){
				LoginUser::redirectToLoginPage("akademik");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_presensi_student");
			$this->subMenu = "Presensi";
			$this->layout ="//layouts/ly_blank";
			$karyawan = $this->loginUser ->karyawan;
			$department_id = $karyawan->department_id;	
			$department_name = $karyawan->department->name;					
			$karyawan_name = $karyawan->name;	
			$karyawan_id=$karyawan->id;				
						
 			$module= $this->loginUser ->module;
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE    group by department_id order by id");

			if ($departments == null){
				$department_id == "";
			}else{	
				$department_id = Yii::app()->request->getParam("department_id", $departments[0]->department_id);
			}
			
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$semester_id = Yii::app()->request->getParam("semester_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
			$absent_date = Yii::app()->request->getParam("absent_date","");				
			

			$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
			}			
		 
            $tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
//            $tingkatan_id = $tingkatans[0]->id ; 
			if ($tingkatans == null){
				$tingkatan_id == "";
			}else{
				$tingkatan_id = $tingkatan_id == "" ? $tingkatans[0]->tingkatan_id : $tingkatan_id;
			}
			
			$mcon= Yii::app()->db;						
			$kelass = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$tingkatan_id,":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}

			$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
			//$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date",date("d-M-Y"))),"d-M-Y");	
			if ($presensi == null){
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date",date("d-M-Y"))),"d-M-Y");	
				$presensi_id=0;
			}else{
				$absent_date = $absent_date == "" ? date('d-M-Y',strtotime($presensi[0]["absent_date"]. "+1 day")) : $absent_date;
				$presensi_id = $presensi[0]["id"];
			}						
 
  
			$data=array(
					"department_id"=>$department_id,
					"departments" => $departments,						
					"karyawan_name"=>$karyawan_name,
					"karyawan_id"=>$karyawan_id,										
					"tahun_ajaran_id"=>$tahun_ajaran_id,
					"tingkatans"=>$tingkatans,							
					"tingkatan_id"=>$tingkatan_id,					
					"tahunAjarans"=>$tahunAjarans,					
					"kelass"=>$kelass,
					"absent_date"=>$absent_date,					
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"department_name"=>$department_name,
					"presensi_id"=>$presensi_id,
					);			
			$this->render('index',$data);
		}
 	
		public function actiondetailList(){	
			
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date","01-Jan-2000")),"Y-m-d");			
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$mcon= Yii::app()->db;
			if ($kelas_tahun_ajaran_id=="") {$kelas_tahun_ajaran_id=0;}					
			$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
			$absent_date_presensi="";
			if ($presensi == null){
				$absent_date_presensi = date_format(date_create_from_format( "Y-m-d", Yii::app()->request->getParam("absent_date",date("d-m-Y"))),"Y-m-d");	
			}else{
				$absent_date_presensi = $absent_date_presensi == "" ? date('Y-m-d',strtotime($presensi[0]["absent_date"])) : $absent_date_presensi;
			}		
 
	$sql1='SELECT B.id as id,VKTA.id as kelas_tahun_ajaran_id  ,VKTA.department_id, VKTA.tahun_ajaran_id,sid, B.name,
case when mpt.id is null then "0" else mpt.id end as stat_id,  
case when mpt.id is null then '.$karyawan_id.' else mpt.karyawan_id end  as karyawan_id,
case when mpt.id is null then null else mpt.presensi_id end  as presensi_id,
case when mpt.id is null then null else mpt.notes end  as notes,
case when mpb.presensi_id is null then null else mpb.absent_date end  as absent_date,
if ( date("'.$absent_date.'") > date(mpb.absent_date) ,"", 
if (mpt.id is null,"Absent",mpt.presensi_status)) as presensi_status
FROM  v_kelas_tahun_ajaran VKTA 
left join  `m_student_class` A on A.kelas_tahun_ajaran_id=VKTA.id 
left join m_student B on A.student_id=B.id
LEFT OUTER JOIN (SELECT P.id as presensi_id,  P.kelas_tahun_ajaran_id, P.absent_date FROM  m_presensi P left join m_presensi_student PS on P.id=PS.presensi_id where  PS.id is not null group by P.id )  mpb on (VKTA.id=mpb.kelas_tahun_ajaran_id)
LEFT OUTER JOIN 
(sELECT P.id as presensi_id,  PS.id as id,P.karyawan_id,P.kelas_tahun_ajaran_id,PS.presensi_status,PS.notes,PS.student_id FROM  `m_presensi` P left join m_presensi_student PS on P.id=PS.presensi_id where  PS.id is not null and PS.absent_date = "'.$absent_date.'"  )  mpt on (mpt.student_id=B.id  and VKTA.id=mpt.kelas_tahun_ajaran_id)
where VKTA.is_active=True and        VKTA.department_id="'.$department_id.'" and VKTA.tahun_ajaran_id ='.$tahun_ajaran_id.'  and   A.kelas_tahun_ajaran_id='.$kelas_tahun_ajaran_id.'     
 ';
 /*
 echo  ($absent_date_presensi).">=". ($absent_date); 
 echo strtotime($absent_date).">".strtotime($absent_date_presensi);
 if (strtotime($absent_date)>=strtotime($absent_date_presensi)){
 echo 'a2';
 }
 else
 {echo 'b1';}
 exit;
 */

 		$mPresensiCommand=$mcon->createCommand($sql1);
			$mPresensiCommand=$mPresensiCommand->queryAll();				
 	
		 	$this->result["actionStatus"] =0;
			$this->result["actionMessage"] =$sql1;
			$this->result["actionError"] =array();
			$this->result["model"] =   $mPresensiCommand;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			 
			 
		}
	 
 
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = 1;//Yii::app()->request->getParam("is_active",1) === "on"?1:0 ;	
			

			if ($op == "add"){
				$presensi_status = Yii::app()->request->getParam("presensi_status","");
 
				if ($presensi_status =="Absent") { }
				else 
				{
				LoginUser::getPermission("akademik_presensi_student_add");
 				$absent_date_last="";
				$stat_insert=0;				
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date","01-Jan-2000")),"Y-m-d");								
				$kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");						
				$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
				if ($presensi == null){
					$presensi_id=0; 
					$absent_date=date('Y-m-d',strtotime($absent_date . "+1 day"));
				}else{
					$absent_date_last = $absent_date_last == "" ? date('d-M-Y',strtotime($presensi[0]["absent_date"])) : $absent_date_last;
					$presensi_id = $presensi[0]["id"];
				}
			
				$absent_date_last_1=date('Y-m-d',strtotime($absent_date_last . "+1 day"));
				
				if (strtotime($absent_date_last_1) == strtotime($absent_date))
				{$stat_insert=1; }
				else
				{
					if (strtotime($absent_date) > strtotime($absent_date_last_1))
					{$stat_insert=0;
						$this->result["actionStatus"] =1;
						$this->result["actionMessage"] ="Data tanggal yang dimasukkan tidak benar, tanggal terakhir " . date('d-M-Y', strtotime($absent_date_last_1));						
					}
					else
					{ 
					 $stat_insert=1; 
					}
				}
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date","01-Jan-2000")),"Y-m-d");			
	 
				//insert date 	/ update							
			 	if ($stat_insert==1) {
				 	if ($presensi_id==0) {	
						$presensi = new MPresensi;					
						$presensi->input_datetime=date("Y-m-d H:i:s");
						$presensi->absent_date=$absent_date;
						$presensi->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
						$presensi->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");		
						$presensi->is_active=$status;
						$presensi->save();		
						$idpresensi = $presensi->getPrimaryKey();														
					}
					else
					{
						$idpresensi = $presensi_id ;	
						if (strtotime($absent_date) == strtotime($absent_date_last_1)){
						$presensi = MPresensi::model()->find("id=:id",array(":id"=>$presensi_id));		
						$presensi->input_datetime=date("Y-m-d H:i:s");
						$presensi->absent_date=$absent_date_last_1;
						$presensi->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
						$presensi->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");		
						$presensi->is_active=$status;
						$presensi->save();	
					 
						}
						else
						{
					 
						}							 
					}			
						 				

					//echo "DATA SUDAH DI ADD........";
					$presensiStudent = new MPresensiStudent;
					$presensiStudent->presensi_id=$idpresensi;	
//					$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date","01-Jan-2000")),"Y-m-d");
					$presensiStudent->student_id= Yii::app()->request->getParam("student_id","");	
					$presensiStudent->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
//					$presensiStudent->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");		
					$presensiStudent->presensi_status=Yii::app()->request->getParam("presensi_status","0");
					$presensiStudent->notes=Yii::app()->request->getParam("notes","-");
					$presensiStudent->absent_date=$absent_date;				
					$presensiStudent->is_active=$status;
					if ($presensiStudent->save()){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="Data sudah diubah";		
					}else{
						print_r($presensiStudent->getErrors());
					}			
					}
				 
				}	
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_presensi_student_edit");
				//print_r ($_POST);
				$presensi_status = Yii::app()->request->getParam("presensi_status","");
 						/*
						$presensi = new MPresensi;					
						$presensi->input_datetime=date("Y-m-d H:i:s");
						$presensi->absent_date=$absent_date;
						$presensi->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
						$presensi->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");		
						$presensi->is_active=$status;
						$presensi->save();		
						*/
				if ($presensi_status=="Absent"){		
				$id = Yii::app()->request->getParam("id","");
					$conn = Yii::app()->db;
					$sql = "delete from m_presensi_student where id = :id";
					$command = $conn->createCommand($sql);
					try{
						//$command->bindParam(":id",$id,PDO::PARAM_INT);
						if ($command->execute(array(":id"=>$id))){
					$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="Data sudah diubah";		
							$this->result["actionError"] =array();
							$this->result["model"] = array();
						}
					}catch(Exception $e){
						$this->result["actionStatus"] =2;
						$this->result["actionMessage"] = $e->getMessage();
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}
				else
				{											
				$id = Yii::app()->request->getParam("id","");				
				$presensi = MPresensiStudent::model()->find("id=:id",array(":id"=>$id));
				$presensi->presensi_status=$presensi_status;
				$presensi->notes=Yii::app()->request->getParam("notes","");					
				if ($presensi->save()){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="Data sudah diubah!";		
				}else{
					print_r($presensi->getErrors());
				}		
				}
		 
			}else if ($op == "add_absent"){
				LoginUser::getPermission("akademik_presensi_student_add");
				//check date
				$absent_date_last="";
				$stat_insert=0;				
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date","01-Jan-2000")),"Y-m-d");								
				$kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");						
				$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
				if ($presensi == null){
					$presensi_id=0; 
					$absent_date=date('Y-m-d',strtotime($absent_date . "+1 day"));
				}else{
					$absent_date_last = $absent_date_last == "" ? date('d-M-Y',strtotime($presensi[0]["absent_date"])) : $absent_date_last;
					$presensi_id = $presensi[0]["id"];
				}
			
				$absent_date_last_1=date('Y-m-d',strtotime($absent_date_last . "+1 day"));
				
				if (strtotime($absent_date_last_1) == strtotime($absent_date))
				{$stat_insert=1; }
				else
				{
					if (strtotime($absent_date) > strtotime($absent_date_last_1))
					{$stat_insert=0;
						$this->result["actionStatus"] =1;
						$this->result["actionMessage"] ="Data tanggal yang dimasukkan tidak valid, data tanggal terakhir presensi ".date('d-M-Y', strtotime($absent_date_last_1));	
					}
					else
					{ 	if ($presensi_id>0) {	$stat_insert=0;	
						$this->result["actionStatus"] =1;
						$this->result["actionMessage"] ="Data tanggal yang dimasukkan tidak valid, data tanggal terakhir presensi ".date('d-M-Y', strtotime($absent_date_last_1));	

						}	else	{$stat_insert=1;}
					}
				}
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date","01-Jan-2000")),"Y-m-d");			
				//insert date 	/ update							
			 	if ($stat_insert==1) {
				 	if ($presensi_id==0) {	
						$presensi = new MPresensi;					
						$presensi->input_datetime=date("Y-m-d H:i:s");										
					}
					else
					{
						$presensi = MPresensi::model()->find("id=:id",array(":id"=>$presensi_id));					 
					}			
					$presensi->absent_date=$absent_date;
					$presensi->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
					$presensi->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");		
					$presensi->is_active=$status;
					if ($presensi->save()){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="Data sudah ditambahkan!";		
						//exit;
					}else{
						print_r($presensi->getErrors());
					}			
 				}
				
			} 
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			
		}
		
		/*
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","singkatan"=>"","keterangan"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){				
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
//				$rsJadwalPelajaran = MJadwalPelajaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				$mcon= Yii::app()->db;
				$sql1='select A.id as id ,A.tahun_ajaran_id,A.kelas_id,A.karyawan_id,A.pelajaran_id,A.status,A.hari,A.pelajaran_ke,A.pelajaran_sampai,A.is_active,A.keterangan,B.tingkatan_id as tingkatan_id from  m_jadwal_pelajaran A left join m_kelas B on A.kelas_id=B.id where A.id='.$id.' order by A.id';
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();	
				$json = CJSON::encode($rsDep);
				$arrdata = json_decode($json,true);
				if (count($arrdata) > 0){
					for ($x=0; $x<count($arrdata); $x++)
					{				
						$data["tahun_ajaran_id"] =$arrdata[$x]['tahun_ajaran_id'];	
						$data["kelas_id"] = $arrdata[$x]['kelas_id'];									
						$data["karyawan_id"] = $arrdata[$x]['karyawan_id'];	
						$data["pelajaran_id"] =$arrdata[$x]['pelajaran_id'];	
						$data["status"] = $arrdata[$x]['status'];			
						$data["hari"] = $arrdata[$x]['hari'];	
						$data["pelajaran_ke"] = $arrdata[$x]['pelajaran_ke'];	
						$data["pelajaran_sampai"] = $arrdata[$x]['pelajaran_sampai'];											
						$data["tingkatan_id"] = $arrdata[$x]['tingkatan_id'];			
						$data["keterangan"] = $arrdata[$x]['keterangan'];																																				
						$data["is_active"] =$arrdata[$x]['is_active'];	
					}
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
		*/		
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>